package com.mantru.iotpot.Adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.KeyEvent
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity.INPUT_METHOD_SERVICE
import com.mantru.iotpot.Adapter.Base.AppAdapter
import com.mantru.iotpot.Adapter.Base.AppHolder
import com.mantru.iotpot.Bean.DishesCreateFoodBean
import com.mantru.iotpot.R
import com.mantru.iotpot.Util.MathUtil
import com.mantru.iotpot.dishes.DishesCreateChooseActivity

class DishesCreateFoodAdapter(var context: Context, layoutResId: Int, var data: List<DishesCreateFoodBean>, var parentPosition: Int): AppAdapter<DishesCreateFoodBean>(context, layoutResId, data) {

    private var dishesCreateFoodListener: DishesCreateFoodListener? = null
    fun setDishesCreateFoodListener(dishesCreateFoodListener: DishesCreateFoodListener?) {
        this.dishesCreateFoodListener = dishesCreateFoodListener
    }
    interface DishesCreateFoodListener {
        fun onDelete(listPosition: Int)
        fun setValue(listPosition: Int, value: String)
    }
    
    override fun convert(holder: AppHolder?, position: Int, item: DishesCreateFoodBean) {
        holder?.setOnClickListener(R.id.adapter_dishes_create_food_value_delete){//监听删除按钮
            dishesCreateFoodListener?.onDelete(position)
        }
            ?.setVisibility(R.id.adapter_dishes_create_food_value_delete,position != 0)
            ?.setOnClickListener(R.id.adapter_dishes_create_food_name){//设置跳转食材选择页事件
                val intent = Intent(context, DishesCreateChooseActivity::class.java).apply {
                    putExtra("existName", item.name)
                    putExtra("parentPosition", parentPosition) // 传递当前项在第几个步骤
                    putExtra("position", position) // 传递当前项的位置
                }
                (context as? Activity)?.startActivityForResult(intent, 1001)
            }
            ?.setText(R.id.adapter_dishes_create_food_name_textview, item.name)
            ?.setText(R.id.adapter_dishes_create_food_value_edittext, item.value)

        val weight = holder?.getView<EditText>(R.id.adapter_dishes_create_food_value_edittext)

        weight!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                //这里要新增逻辑，如果输入了0开头，而非0的数据，要把开头的0去掉
                dishesCreateFoodListener?.setValue(holder.layoutPosition, MathUtil.trimLeadingZeros(s.toString()))
            }

            override fun afterTextChanged(s: Editable?) {}
        })

        weight.setOnEditorActionListener { v: TextView, actionId: Int, event: KeyEvent? ->
            // 检查是否是按下 Enter 键或者完成操作
            if ((event != null && event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER)
                || actionId == android.view.inputmethod.EditorInfo.IME_ACTION_DONE
                || actionId == android.view.inputmethod.EditorInfo.IME_ACTION_NEXT
                || actionId == android.view.inputmethod.EditorInfo.IME_ACTION_SEND) {

                // 获取输入法管理器
                val imm = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager?
                // 隐藏软键盘
                imm?.hideSoftInputFromWindow(v.windowToken, 0)
                // 让输入框失去焦点
                v.clearFocus()
                return@setOnEditorActionListener true
            }
            return@setOnEditorActionListener false
        }
    }
}